import { Breadcrumb as AntBreadcrumb } from 'antd';
import React from 'react';
import { useConfig } from '../ConfigProvider';
import BreadcrumbItem from './BreadcrumbItem';
import { BreadcrumbComponent } from './interface';
import './style.less';

const Breadcrumb: BreadcrumbComponent = (props) => {
  const { antNative, hideLastItem, children, ...restProps } = props;
  const { antNative: globalAntNative } = useConfig();

  if (antNative || globalAntNative) {
    return <AntBreadcrumb {...restProps}>{children}</AntBreadcrumb>;
  }

  // 处理 hideLastItem
  if (hideLastItem && React.Children.count(children) > 0) {
    const childrenArray = React.Children.toArray(children);
    const visibleChildren = childrenArray.slice(0, -1);
    return <AntBreadcrumb {...restProps}>{visibleChildren}</AntBreadcrumb>;
  }

  return <AntBreadcrumb {...restProps}>{children}</AntBreadcrumb>;
};

Breadcrumb.Item = BreadcrumbItem;

export default Breadcrumb;
